package factory_pattern.factory_method;

import factory_pattern.factory_method.factory.TcpFactory;
import factory_pattern.factory_method.factory.UdpFactory;

// 客户端使用示例
public class Client {
    public static void main(String[] args) {
        // 使用TCP工厂创建TCP连接
        ConnectionFactory tcpFactory = new TcpFactory();
        NetworkConnection tcpConn = tcpFactory.createConnection("127.0.0.1", "8080");
        if (tcpConn.connect()) {
            tcpConn.sendData("TCP: Hello Server".getBytes());
            tcpConn.close();
        }

        System.out.println("----------------------------------------");

        // 使用UDP工厂创建UDP连接
        ConnectionFactory udpFactory = new UdpFactory();
        NetworkConnection udpConn = udpFactory.createConnection("127.0.0.1", "8081");
        if (udpConn.connect()) {
            udpConn.sendData("UDP: Hello Server".getBytes());
            udpConn.close();
        }

        System.out.println("----------------------------------------");
    }
}
